﻿using Caliburn.Micro;
using SS.WPF.Framework.Display.MsgBox;
using SS.WPF.Framework.Localization;

namespace SS.WPF.Framework.Display.Dialog
{
    using System;

    public static partial class DialogManagerExtensions
    {
        #region Constants and Fields

        /// <summary>
        /// TITLE message key
        /// </summary>
        public static readonly string TITLE = "EFPConfigurationTool";

        #endregion

        #region Properties


        #endregion

        #region Public Methods

        /// <summary>
        /// Show confirm message 
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="key">
        /// The message.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <returns>
        /// MsgBoxResult OK/Cancel
        /// </returns>
        public static MsgBoxResult ShowConfirmByKey(this IDialogManager dialogManager, string key, string title, params string[] args)
        {
            title = string.IsNullOrEmpty(title) ? TITLE : title;
            var message = IoC.Get<TranslationManager>().Translate(key) as string;
            if (message != null && args.Length > 0)
            {
                message = string.Format(message, args);
            }

            var info = new MsgBoxInfo
            {
                Title = IoC.Get<TranslationManager>().Translate(title) as string,
                Buttons = MsgBoxButtons.YesNo,
                Heading = message,
                PreferenceKey = key,
                Image = MsgBoxImage.Question
            };

            return ShowMsgBox(dialogManager, info);
        }

        /// <summary>
        /// Show confirm message 
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="key">
        /// The message.
        /// </param>
        /// <returns>
        /// MsgBoxResult OK/Cancel
        /// </returns>
        public static MsgBoxResult ShowConfirmByKey(this IDialogManager dialogManager, string key, params object[] args)
        {
            return ShowConfirmByKey(dialogManager, key, null, false, true, args);
        }

        public static MsgBoxResult ShowConfirmByKeyWithoutState(this IDialogManager dialogManager, string key, params object[] args)
        {            
            return ShowConfirmByKey(dialogManager, key, null, false, false, args);
        }

        /// <summary>
        /// Show confirm message 
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <param name="cancelable"></param>
        /// <param name="args"></param>
        /// <returns>
        /// MsgBoxResult OK/Cancel
        /// </returns>
        public static MsgBoxResult ShowConfirmByKey(this IDialogManager dialogManager,
            string key, string title, bool cancelable, bool storePreference, params object[] args)
        {
            title = string.IsNullOrEmpty(title) ? TITLE : title;

            var message = IoC.Get<TranslationManager>().Translate(key) as string;
            if (message != null && args.Length > 0)
            {
                message = string.Format(message, args);
            }
            var info = new MsgBoxInfo
            {
                Title = IoC.Get<TranslationManager>().Translate(title) as string,
                Buttons = cancelable ? MsgBoxButtons.YesNoCancel : MsgBoxButtons.YesNo,
                Heading = message,
                PreferenceKey = key,
                Image = MsgBoxImage.Question,
                StorePreference = storePreference,
            };

            return ShowMsgBox(dialogManager, info);
        }



        /// <summary>
        /// Show confirm message 
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="cancelable"></param>
        /// <returns>
        /// MsgBoxResult OK/Cancel
        /// </returns>
        public static MsgBoxResult ShowConfirmByKey(this IDialogManager dialogManager,
            string message, bool cancelable, params object[] args)
        {
            return ShowConfirmByKey(dialogManager, message, null, cancelable, true, args);
        }

        public static MsgBoxResult ShowConfirmByKeyWithoutState(this IDialogManager dialogManager,
            string message, bool cancelable, params object[] args)
        {
            return ShowConfirmByKey(dialogManager, message, null, cancelable, false, args);
        }

        /// <summary>
        /// Show error message 
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="key">
        /// The message.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <returns>
        /// MsgBoxResult OK
        /// </returns>
        public static MsgBoxResult ShowErrorByKeyWithTitle(this IDialogManager dialogManager, string key, string title, params object[] args)
        {
            title = string.IsNullOrEmpty(title) ? TITLE : title;
            var message = IoC.Get<TranslationManager>().Translate(key) as string;
            if (message != null && args.Length > 0)
            {
                message = string.Format(message, args);
            }
            var info = new MsgBoxInfo
            {
                Title = IoC.Get<TranslationManager>().Translate(title) as string,
                Buttons = MsgBoxButtons.OK,
                Heading = message,
                PreferenceKey = key,
                Image = MsgBoxImage.Error
            };

            return ShowMsgBox(dialogManager, info);
        }

        /// <summary>
        /// Show error message 
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <returns>
        /// MsgBoxResult OK
        /// </returns>
        public static MsgBoxResult ShowErrorByKey(this IDialogManager dialogManager, string message, params object[] args)
        {
            return ShowErrorByKeyWithTitle(dialogManager, message, null, args);
        }

        /// <summary>
        /// Show error message 
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <returns>
        /// MsgBoxResult OK
        /// </returns>
        public static MsgBoxResult ShowError(this IDialogManager dialogManager, string message)
        {
            var title = TITLE;

            var info = new MsgBoxInfo
            {
                Title = IoC.Get<TranslationManager>().Translate(title) as string,
                Buttons = MsgBoxButtons.OK,
                Heading = message,
                Image = MsgBoxImage.Error
            };

            return ShowMsgBox(dialogManager, info);
        }

        /// <summary>
        /// Show error message 
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <returns>
        /// MsgBoxResult OK
        /// </returns>
        public static MsgBoxResult ShowInfo(this IDialogManager dialogManager, string message)
        {
            var title = TITLE;

            var info = new MsgBoxInfo
            {
                Title = IoC.Get<TranslationManager>().Translate(title) as string,
                Buttons = MsgBoxButtons.OK,
                Heading = message,
                Image = MsgBoxImage.Information
            };

            return ShowMsgBox(dialogManager, info);
        }

        /// <summary>
        /// Show confirm message 
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <returns>
        /// MsgBoxResult OK
        /// </returns>
        public static MsgBoxResult ShowInfoByKey(this IDialogManager dialogManager, string key, string title, params object[] args)
        {
            title = string.IsNullOrEmpty(title) ? TITLE : title;
            var message = IoC.Get<TranslationManager>().Translate(key) as string;
            if (message != null && args.Length > 0)
            {
                message = string.Format(message, args);
            }
            var info = new MsgBoxInfo
            {
                Title = IoC.Get<TranslationManager>().Translate(title) as string,
                Buttons = MsgBoxButtons.OK,
                Heading = message,
                PreferenceKey = key,
                Image = MsgBoxImage.Information
            };

            return ShowMsgBox(dialogManager, info);
        }

        /// <summary>
        /// Show confirm message 
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <returns>
        /// MsgBoxResult OK
        /// </returns>
        public static MsgBoxResult ShowInfoByKey(this IDialogManager dialogManager, string message, params object[] args)
        {
            return ShowInfoByKey(dialogManager, message, null, args);
        }


        /// <summary>
        /// Show message box
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager
        /// </param>
        /// <param name="info">
        /// Message box information
        /// </param>
        /// <returns>
        /// The shown result
        /// </returns>
        public static MsgBoxResult ShowMsgBox(this IDialogManager dialogManager, MsgBoxInfo info)
        {
            if (info == null)
            {
                throw new ArgumentException("Cannot show message box with null MsgBoxInfo");
            }

            info.Title = info.Title ?? TITLE;
            return dialogManager.ShowMessageBox(info);
        }

        /// <summary>
        /// Show warning message
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="message">
        /// The message to show
        /// </param>
        /// <param name="title">
        /// The title of this message box
        /// </param>
        /// <returns>
        /// MsgBoxResult OK
        /// </returns>
        public static MsgBoxResult ShowWarningByKey(this IDialogManager dialogManager, string key, string title, params object[] args)
        {
            title = string.IsNullOrEmpty(title) ? TITLE : title;
            var message = IoC.Get<TranslationManager>().Translate(key) as string;
            if (message != null && args.Length > 0)
            {
                message = string.Format(message, args);
            }
            var info = new MsgBoxInfo
            {
                Title = IoC.Get<TranslationManager>().Translate(title) as string,
                Buttons = MsgBoxButtons.OK,
                Heading = message,
                PreferenceKey = key,
                Image = MsgBoxImage.Warning
            };

            return ShowMsgBox(dialogManager, info);
        }

        /// <summary>
        /// Show warning message
        /// </summary>
        /// <param name="dialogManager">
        /// The dialog manager.
        /// </param>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <returns>
        /// MsgBoxResult OK
        /// </returns>
        public static MsgBoxResult ShowWarningByKey(this IDialogManager dialogManager, string message, params object[] args)
        {
            return ShowWarningByKey(dialogManager, message, null, args);
        }

        #endregion
    }
}